//
//  VisitorTableViewController.swift
//  Sina
//
//  Created by maling  on 15/11/4.
//  Copyright © 2015年 maling . All rights reserved.
//

import UIKit
import SnapKit
class VisitorTableViewController: UITableViewController {

    //用户登录标记
    private var userLogin = UserAccountViewModel.sharedUserAccount.userLogin
//    private var userLogin = UserAccountViewModel().userLogin
    //     private var userLogin = UserAccountViewModel().userAccount?.access_token != nil
    
    
    //定义一个属性
    var visitorView: VisitorView?
    
    //加载视图显示哪一个
    override func loadView() {
        
        userLogin ? super.loadView() : setupVisitorView()
    }
    
//    override func viewDidAppear(animated: Bool) {
//        super.viewDidAppear(animated) 
//        
//        print(visitorView)
//    }

   
    private func setupVisitorView() {
    
        //替换跟视图  给属性赋值
        visitorView = VisitorView()

        view = visitorView
        
        //控制器成为（监听注册登录按钮）代理--必须实现代理方法否则出错
        visitorView?.delegate = self
        
        
        //MARK: - 给注册登录按钮添加监听方法
        visitorView?.registerButton.addTarget(self, action: "visitorViewDidLogin", forControlEvents: .TouchUpInside)
        visitorView?.loginButton.addTarget(self, action: "visitorViewDidRegister", forControlEvents: .TouchUpInside)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "visitorViewDidRegister")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "确定", style: .Plain, target: self, action: "visitorViewDidLogin")
        
        
        //MARK: 需要自定义一个UIView 在里面显示需要观察者看到的内容
//        visitorView?.backgroundColor = UIColor.orangeColor()
        
    }
}
//MARK: - 代理方法
extension VisitorTableViewController: VisitorViewDelegate {

    func visitorViewDidLogin() {
        
        print("登录。。。。")
    }

    func visitorViewDidRegister() {
//        print("注册。。。。")
        
        //MARK: 授权页面
        let vc = OAuthViewController()
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
}



